<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      margin: 0 auto;
      width: 12.5rem;
      padding: 1.5rem;
      background: #f5222d;
      text-align: center;
    
      .day {
        font-size: 1.25rem;
        color: #fff;
        margin-bottom: 1.5rem;
      }

      p {
        font-size: .875rem;
        color: #fff;
        margin-bottom: 1rem;
      }

      .time_box {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 1.5rem;
        color: #fff;

       .card {
          width: 2.5rem;
          height: 2.5rem;
          background: #333;
          line-height: 2.5rem;
          margin: 0 .5rem;
          border-radius: .25rem;
        } 
      }

      .end {
        font-size: .875rem;
        color: #fff;
      }
    }
  </style>
</head>
<body>
    <div class="box">
        <div class="day">2024-04-26</div>
        <p>下班倒计时</p>
        <div class="time_box">
            <div class="hour card">18</div> :
            <div class="minute card">00</div> :
            <div class="second card">00</div>
        </div>
        <div class="end">18:30:00下课</div>
    </div>
    <script>
    let timer

function getCountTime() {
  const date = new Date()
  const dateDay = document.querySelector('.day')
  dateDay.innerHTML = date.toLocaleDateString().replaceAll('/', '-')
  
  const nowDate = date.getTime()
  // 设置一个未来的目标时间
  const endDate = new Date(date.toLocaleDateString() + ' 18:00:00').getTime()
  
  // 如果当前时间已过下班时间，设置为明天的下班时间
  if (nowDate > endDate) {
    endDate.setDate(endDate.getDate() + 1)
  }
  
  let times = (endDate - nowDate) / 1000
  
  if (times > 0) {
    const [hour, minute, second] = formatTime(times)
    document.querySelector('.hour').innerHTML = hour
    document.querySelector('.minute').innerHTML = minute
    document.querySelector('.second').innerHTML = second
  } else {
    clearInterval(timer)
    document.querySelector('.hour').innerHTML = '00'
    document.querySelector('.minute').innerHTML = '00'
    document.querySelector('.second').innerHTML = '00'
  }
}

function formatTime(s) {
  const hour = String(parseInt(s / 60 / 60 % 24)).padStart(2, '0')
  const minute = String(parseInt(s / 60 % 60)).padStart(2, '0')
  const second = String(parseInt(s % 60)).padStart(2, '0')
  return [hour, minute, second]
}

  getCountTime()
  timer = setInterval(getCountTime, 1000)
</script>
</body>
</html>